package edu.winona.csclub.testing.core;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

import edu.winona.csclub.testing.utils.XmlUtils;

public class ConfigFile {
	File source ;
	 Document doc=null;
	 
	public ConfigFile(File filename){
		source=filename;
	}
	
	public List<Node> getCommandNodes(){
		checkInit();
	    Element testCase = doc.getDocumentElement();
		return XmlUtils.getCommands(testCase);
	}
	
	public List<Command> getCommands(){
		List<Command> commands = new LinkedList<Command>();
		for( Node command : getCommandNodes() ){
			List<Variable> variables = XmlUtils.getVariables( command );
			commands.add(new Command(command.getAttributes().getNamedItem("value").getNodeValue(),variables));
		}
		return commands;
	}
	
	private void checkInit(){
		if(doc!=null)return;
		  DOMParser p = new DOMParser();
	      try {
			p.parse(source.getPath());
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	       doc = p.getDocument();
	}
}
